<?php
/*
    
    Copyright (C)  2012 Sweta ray.
    Permission is granted to copy, distribute and/or modify this document
    under the terms of the GNU Free Documentation License, Version 1.3
    or any later version published by the Free Software Foundation;
    with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
    A copy of the license is included in the section entitled "GNU
    Free Documentation License"
	@license GNU/GPL http://www.gnu.org/copyleft/gpl.html
    Questions for Joomla
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
   
	Questions for Joomla
	Version 0.0.1
	Created date: Sept 2012
	Creator: Sweta Ray
	Email: admin@phpseo.net
	support: support@phpseo.net
	Website: http://www.phpseo.net
*/

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.error.log');

class CustomLogger {
	
	//Variables
	private $_handle;
	private $_loglevel;
	private $_logfile;
	private $_options;
	
	//Constants
	const LOG_INFO 		= '9';
	const LOG_WARNING 	= '7';
	const LOG_ERROR		= '5';
	const LOG_SEVERE 	= '1';
	const LOG_NOLOG		= '0';
	
	function __construct( $filename = 'com_questions.log' , $loglevel = 0 , $format = '{DATE}_{TIME}_{USER_ID}_{CLASSNAME}: {COMMENT}' ){ 
		$this->_loglevel = $loglevel;
		$this->_logfile = $filename . ".php";	
		$this->_options = array(
	   		'format' => $format
		);
	}
	
	function setLoglevel( $level ){
		$this->_loglevel = $level;
	}
	
	function getLoglevel (){
		return $this->_loglevel;
	}
	
	function info($message){
	if ($this->_loglevel >= self::LOG_INFO){
			$this->_log($message , "INFO");
		}
	}
	
	function warning($message){
	if ($this->_loglevel >= self::LOG_WARNING){
			$this->_log($message , "WARNING");
		}
	}
	
	function error($message){
	if ($this->_loglevel >= self::LOG_ERROR){
			$this->_log($message , "ERROR");
		}
	}
	
	function severe($message){
	if ($this->_loglevel >= self::LOG_SEVERE){
			$this->_log($message , "SEVERITY");
		}
	}
	
	private function _log($message , $loglevel , $class = NULL){
		$userId = JFactory::getUser()->id;
		$log = JLog::getInstance( $this->_logfile , $this->_options );
		$log->addEntry( array('comment' => $message , 'user_id' => $userId , 'classname' => $class ));
	}
	
}
